﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Tongiao.Entity;
using Tongiao.Manager;
using Tongiao.ManagerBase;
using WB.Library;
using WB.Entity.Helper;


public partial class admin_News_AddEditNews : WB.Controls.UcControls
{

    public Guid NewsId
    {
        //get { return FGuid.ToGuid(Request["id"]); }

        get
        {
            if (FGuid.IsGuid(hidNewsId.Value))
                return new Guid(hidNewsId.Value);
            return Guid.Empty;
        }
        set
        {
            hidNewsId.Value = value.ToString();
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            hidNewsId.Value = Request["id"];
            if (hidNewsId.Value == "")
                btnAddNews.Text = "Đăng";
            else
                btnAddNews.Text = "Cập nhật";
            LoadCategory();
            LoadDetail();

        }
    }

    protected void btnAddNews_Click(object sender, EventArgs e)
    {
        if (hidNewsId.Value == "")
        {

            Insert();
            btnAddNews.Text = "Cập nhật";


        }
        else
        {
            btnAddNews.Text = "Cập nhật";
            Update();
        }
        
    }

    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("?control=NNewsList");
    }

    void LoadCategory()
    {
        string catType = "NEWS";
        DataTable dtCat = Tg_CategoryManager.CreateInstant().getAllCategoryListByCatType(catType);
        if (dtCat.Rows.Count > 0)
        {
            ddlVCategory.DataSource = dtCat;
            ddlVCategory.DataTextField = "CatNameChild";
            ddlVCategory.DataValueField = "IdChild";
            ddlVCategory.DataGroupField = "CatName";
            ddlVCategory.DataBind();
        }
    }

    void Insert()
    {
        Tg_NewsInfosEntity objNews = new Tg_NewsInfosEntity();
        objNews.CategoryId = new Guid(ddlVCategory.SelectedValue);
        objNews.Title = txtTitle.Text;
        objNews.Absolute = txtInfor.Text;
        objNews.ContentBody = txtContent.Value.ToString();
        if (filePhoto.HasFile)
            objNews.Photo = FileUploadControl.FullPath(filePhoto, txtTitle.Text, ProductsField.Photo.MaxLength, EnumsFile.Files); //FileUploadControl.FullPath(filePhoto,NewsField.Photo.MaxLength);
        objNews.CreatedBy = Profile.UserName;
        objNews.CreatedDate = DateTime.Now;

        Tg_NewsInfosManager.CreateInstant().Insert(objNews);
        Response.Redirect("?control=" + NewsId.ToString());
        CustomValidator1.ErrorMessage = "Đã thêm dữ liệu thành công.";
        CustomValidator1.IsValid = false;

    }

    void Update()
    {
        Tg_NewsInfosEntity objNews = new Tg_NewsInfosEntity();
        objNews.Id = NewsId;
        objNews.CategoryId = new Guid(ddlVCategory.SelectedValue);
        objNews.Title = txtTitle.Text;
        objNews.Absolute = txtInfor.Text;
        objNews.ContentBody = txtContent.Value.ToString();
        objNews.UpdatedBy = Profile.UserName;
        objNews.UpdatedDate = DateTime.Now;
        if (filePhoto.HasFile)
            objNews.Photo = FileUploadControl.FullPath(filePhoto, txtTitle.Text, ProductsField.Photo.MaxLength, EnumsFile.Files);

        Tg_NewsInfosManager.CreateInstant().Update(objNews);
        CustomValidator1.ErrorMessage = "Đã cập nhật dữ liệu thành công.";
        CustomValidator1.IsValid = false;
    }

    protected void LoadDetail()
    {
        if (NewsId != Guid.Empty)
        {
           
            DataTable tbl = Tg_NewsInfosManager.CreateInstant().SelectById(NewsId);
            if (tbl.Rows.Count > 0)
            {

                hidNewsId.Value = tbl.Rows[0]["Id"].ToString();
                txtTitle.Text = tbl.Rows[0]["Title"].ToString();
                txtContent.Value = tbl.Rows[0]["ContentBody"].ToString();
                txtInfor.Text = tbl.Rows[0]["Absolute"].ToString();
                if (tbl.Rows[0]["CategoryId"].ToString() != "")
                {
                    ddlVCategory.SelectedValue = tbl.Rows[0]["CategoryId"].ToString();
                }
                if (!string.IsNullOrEmpty(tbl.Rows[0]["Photo"].ToString()))
                {
                    imgPhoto.Visible = true;
                    imgPhoto.ImageUrl = tbl.Rows[0]["Photo"].ToString();
                }
              }
        }
    }
}
